<template>
  <view class="tab">
    <view
      class="item"
      v-for="(item, index) in tabs"
      :key="index"
      :style="{ width: itemWidth }"
      @click="change(index, item)"
    >
      <span class="name" :class="{active: current === index}">{{ item.name }}</span>
    </view>

    <view
      class="line-wrap"
      :style="{
        width: itemWidth,
        bottom: lineBottom,
        transform: `translateX(${current}00%)`,
      }"
    >
      <view class="line" :style="lineStyle"></view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "Tab",
    props: {
      tabs: Array,
      lineBottom: {
        type: String,
        default: '8px'
      },
      lineStyle: Object
    },
    computed: {
      itemWidth() {
        return `${100 / this.tabs.length}%`;
      },
    },
    data() {
      return {
        current: 0,
      };
    },
    methods: {
      change(index, item) {
        this.current = index
        this.$emit('change', {item, index})
      }
    },
  };
</script>

<style lang="scss" scoped>
  @import './index.scss';
</style>
